package com.kgc.ebook.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.ebook.mapper.CategoryMapper;
import com.kgc.ebook.mapper.EntryMapper;
import com.kgc.ebook.pojo.Category;
import com.kgc.ebook.pojo.Entry;
import com.kgc.ebook.pojo.EntryExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EntryServiceImpl implements EntryService {

    @Resource
    CategoryMapper categoryMapper;
    @Resource
    EntryMapper entryMapper;
    @Override
    public PageInfo<Entry> selectAll(Integer pageNum, Integer categoryid) {
        PageHelper.startPage(pageNum,3);
        List<Entry> entries=new ArrayList<>();
        if (categoryid!=0){
            EntryExample example=new EntryExample();
            EntryExample.Criteria criteria=example.createCriteria();
            criteria.andCategoryidEqualTo(categoryid);
            entries=entryMapper.selectByExample(example);
        }else{
            entries=entryMapper.selectByExample(null);
        }
        PageInfo<Entry> pageInfo=new PageInfo<>(entries);
        return pageInfo;
    }

    @Override
    public List<Category> selectAllCategory() {
        List<Category> categoryList = categoryMapper.selectByExample(null);
        return categoryList;
    }

    @Override
    public Integer saveEntry(Entry entry) {
        return entryMapper.insertSelective(entry);
    }

    @Override
    public Integer deleteEntry(Integer id) {
        return entryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Entry selectAllByEntry(Integer id) {
        return entryMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updateEntry(Entry entry) {
        return entryMapper.updateByPrimaryKeySelective(entry);
    }


}
